/**
 * 
 */
package com.vision.core.cm.data;

import java.util.HashSet;
import java.util.Set;

import com.vision.core.cm.db.data.Permission;
import com.vision.core.cm.db.data.Role;
import com.vision.core.cm.db.data.User;

/**
 * @author Mark
 *
 */
public class RoleUtils {
	
	public static boolean hasAny(User user, Set<Permission> permissions) {
		Set<Role> roles = user.getRoles();
		for (Role role : roles) {
			if (hasAny(role, permissions))
				return true;
		}
		return false;
	}
	
	public static boolean hasAny(User user, Permission ... permissions) {
		Set<Role> roles = user.getRoles();
		for (Role role : roles) {
			if (hasAny(role, permissions))
				return true;
		}
		return false;
	}
	
	public static boolean hasAll(User user, Permission ... permissions) {
		Set<Role> roles = user.getRoles();
		Set<Permission> rolePermissions = new HashSet<Permission>();
		for (Role role : roles) {
			rolePermissions.addAll(role.getPermissions());
		}
		for (Permission permission : permissions) {
			if (!rolePermissions.contains(permission))
				return false;
		}
		return true;
	}
	
	public static boolean hasAny(Role role, Permission ... permissions) {
		Set<Permission> p = role.getPermissions();
		for (Permission permission : permissions)
			if (p.contains(permission))
				return true;
		return false;
	}
	
	public static boolean hasAny(Role role, Set<Permission> permissions) {
		Set<Permission> p = role.getPermissions();
		for (Permission permission : permissions)
			if (p.contains(permission))
				return true;
		return false;
	}
	
	public static boolean hasAll(Role role, Permission ... permissions) {
		Set<Permission> p = role.getPermissions();
		for (Permission permission : permissions)
			if (!p.contains(permission))
				return false;
		return true;
	}

}
